package zmee.pur.src.formplugin;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;

import java.util.EventObject;

/**
 * @Author: xw
 * @Date: 2023/02/14/11:06
 * @Description: 供应商分配及信息变更单
 */
public class AllotAndInfoChangeBillPlugin extends AbstractBillPlugIn {

    @Override
    public void afterBindData(EventObject e) {
        String status = this.getView().getFormShowParameter().getStatus().name();
        if ("ADDNEW".equals(status)) {
            Object creator = this.getModel().getValue("creator");
            this.getModel().setValue("zmee_applicant", creator);
            DynamicObjectCollection entryEntity = ((DynamicObject) creator).getDynamicObjectCollection("entryentity");
            for (DynamicObject object : entryEntity) {
                if (!object.getBoolean("ispartjob")) {
                    this.getModel().setValue("zmee_applydept", object.get("dpt"));
                    break;
                }
            }
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String name = e.getProperty().getName();
        ChangeData[] changeSet = e.getChangeSet();
        Object newValue = changeSet[0].getNewValue();
        if ("zmee_applicant".equals(name) && newValue != null) {
            DynamicObjectCollection entryEntity = ((DynamicObject) newValue).getDynamicObjectCollection("entryentity");
            for (DynamicObject object : entryEntity) {
                if (!object.getBoolean("ispartjob")) {
                    this.getModel().setValue("zmee_applydept", object.get("dpt"));
                    break;
                }
            }
        }
    }
}
